/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session;

import entities.CompteBancaire;
import entities.TypeCompte;
import entities.Users;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Cyn
 */
@Stateless
@LocalBean
public class GestionnaireUsers {
    @PersistenceContext(unitName = "ARTP3-ejbPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }
    
    public Users authentification(String log, String pwd) {
       
        
        try
       {
           System.out.println("==Authentifier==");
            Query query = em.createQuery("select u from Users u where u.login =:login and u.pwd =:pass");
            query.setParameter("login", log);
            query.setParameter("pass", pwd);
            Users u = (Users) query.getSingleResult();
            System.out.println("login found : "+u.getLogin());
            return u;
       }
       catch(Exception e)
       {
           return null;
       }
       
    }
    
    public Users trouverLogin(String login){
        Query query = em.createNamedQuery("Users.findByLogin");
        query.setParameter("login", login);
        
        try{            
            System.out.println("==SessionBean==");
            return (Users) query.getSingleResult(); 
        }catch(Exception e){
            System.out.println("Login inexistant");
            return null;
        }
           
    }
    
    public Users getUserById(Long i){
        Query query = em.createNamedQuery("Users.findById");
        query.setParameter("userid", i);        
        try{            
            System.out.println("==SessionBean==");
            return (Users) query.getSingleResult(); 
        }catch(Exception e){
            System.out.println("Login inexistant");
            return null;
        }
    }
    
    public Users getUserByCompte(CompteBancaire c){
        for(Users u : getAllUsers()){
            if(u.getComptes().contains(c)){
                return u;
            }
        }
        
        return null;
    }
    
    
    public List<Users> getUsersBySearch(String mot) {
        Query query = em.createNamedQuery("Users.search");
        query.setParameter("mot", "%"+mot+"%"); 
        return query.getResultList();
    }
    
    public List<Users> getAllUsers() {
        Query query = em.createNamedQuery("Users.findAll");
        return query.getResultList();
    }
    
    public void ajouter(Users u){
        em.persist(u);
    }
    
    public Users update(Users u){
        return em.merge(u);
    }
    
    public void delete(Users u) {
        System.err.println("delete nom :"+u.getLogin());
        Users us = getUserById(u.getId());
        em.remove(us);
    }
    
    public void init()
    {
        Users u;
        CompteBancaire cb1;
        CompteBancaire cb2;
        CompteBancaire cb3;
        CompteBancaire cb4;
        TypeCompte cc = new TypeCompte("Compte courant");
        TypeCompte pea = new TypeCompte("PEA");
        TypeCompte la = new TypeCompte("Livret A");
        TypeCompte cj = new TypeCompte("Compte jeune");
        for(int i=0 ; i<200 ; i++)
        {
            u = new Users("user"+i, "user"+i, "user"+i, "Client");
            cb1 = new CompteBancaire(10000, cc);
            cb2 = new CompteBancaire(10000, pea);
            cb3 = new CompteBancaire(10000, la);
            cb4 = new CompteBancaire(10000, cj);
            u.addCompte(cb1);
            u.addCompte(cb2);
            u.addCompte(cb3);
            u.addCompte(cb4);
            em.persist(u);
            
        }
        em.persist(new Users("toto", "toto", "toto", "Client"));
        em.persist(new Users("admin", "admin", "admin", "Admin"));
    }
    
    
    
    
}
